package com.foreknow.controller;

import com.foreknow.model.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller

// 类似总路由
@RequestMapping("/helloController")
public class HelloController {

//    public void list(HttpServletRequest req,HttpServletResponse resp){
//        req.setAttribute("list",list); item=""
//    }
//    作用同上

    //value类似子路由
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String printHello(ModelMap model) {
        //在服务器端保存数据 addAttribute(key,value)
        model.addAttribute("message","Hello Spring MVC Framework!");
        return "hello";
    }

    @RequestMapping(value = "/student", method = RequestMethod.GET)
    public ModelAndView mavStudent() {
        return new ModelAndView("Student", "command", new Student());
    }
    @RequestMapping(value = "/addStudent", method = RequestMethod.POST)
    public String addStudent(@ModelAttribute("Student") Student student,
                             ModelMap model) {

        model.addAttribute("name",student.getName());
        model.addAttribute("age", student.getAge());
        model.addAttribute("id", student.getId());
        return "result";
    }

}
